import { _decorator, Component, Node, Label, director } from 'cc';
import { SSS_AudioManager } from 'db://assets/Script/Manager/SSS_AudioManager';
import { JiuJiuXiaoDongWu_GameStorage } from '../Data/JiuJiuXiaoDongWu_GameStorage';
import JiuJiuXiaoDongWu_GameManager from '../JiuJiuXiaoDongWu_GameManager';
import AdManager from 'db://assets/Script/Manager/AdManager';
import SSS_GameEventManager, { SS_GameEventData } from 'db://assets/Script/Manager/SSS_GameEventManager';
import { JiuJiuXiaoDongWu_GameData } from '../Data/JiuJiuXiaoDongWu_GameData';
import { SSS_Consts } from 'db://assets/Script/Data/SSS_Consts';
import { SSS_Lockable } from 'db://assets/Script/Common/SSS_Lockable';
import SSS_ShuaiFunction from 'db://assets/Script/Utils/SSS_ShuaiFunction';

const { ccclass, property } = _decorator;

@ccclass('JiuJiuXiaoDongWu_GamePanel')
export default class JiuJiuXiaoDongWu_GamePanel extends Component {
    static Instance: JiuJiuXiaoDongWu_GamePanel;

    @property(Node)
    Button_back: Node | null = null;
    @property(Node)
    Button_refresh: Node | null = null;

    @property(Node)
    Button_撤回: Node | null = null;
    @property(Node)
    Button_翻转: Node | null = null;

    @property(Label)
    Label_撤回_number: Label | null = null;
    @property(Node)
    撤回广告icon: Node | null = null;

    @property(Label)
    Label_翻转_number: Label | null = null;
    @property(Node)
    翻转广告icon: Node | null = null;



    onLoad() {
        JiuJiuXiaoDongWu_GamePanel.Instance = this;
    }

    ShowOn() {
        this.node.active = true;
        this.AddListener();
        this.Refresh();
    }
    ShowOff() {
        this.node.active = false;
        this.RemoveListener();
    }
    Refresh() {
        this.Label_撤回_number.string = `${JiuJiuXiaoDongWu_GameStorage.data.mode_1.item_撤回_number}`;
        this.Label_翻转_number.string = `${JiuJiuXiaoDongWu_GameStorage.data.mode_1.item_翻转_number}`;

        this.撤回广告icon.active = JiuJiuXiaoDongWu_GameStorage.data.mode_1.item_撤回_number <= 0;
        this.翻转广告icon.active = JiuJiuXiaoDongWu_GameStorage.data.mode_1.item_翻转_number <= 0;
    }
    AddListener() {
        this.Button_撤回.on(Node.EventType.TOUCH_END, this.On_Button_撤回, this);
        this.Button_翻转.on(Node.EventType.TOUCH_END, this.On_Button_翻转, this);
        this.Button_back.on(Node.EventType.TOUCH_END, this.On_Button_back, this);
        this.Button_refresh.on(Node.EventType.TOUCH_END, this.On_Button_refresh, this);
    }
    RemoveListener() {
        this.Button_撤回.off(Node.EventType.TOUCH_END, this.On_Button_撤回, this);
        this.Button_翻转.off(Node.EventType.TOUCH_END, this.On_Button_翻转, this);
        this.Button_back.off(Node.EventType.TOUCH_END, this.On_Button_back, this);
        this.Button_refresh.off(Node.EventType.TOUCH_END, this.On_Button_refresh, this);
    }

    @SSS_Lockable(1000)
    On_Button_撤回() {
        if (JiuJiuXiaoDongWu_GameStorage.data.mode_1.item_撤回_number > 0) {
            JiuJiuXiaoDongWu_GameStorage.data.mode_1.item_撤回_number--;
            JiuJiuXiaoDongWu_GameStorage.saveData();
            this.Refresh();

            JiuJiuXiaoDongWu_GameManager.Instance.撤回();
        }
        else {
            SSS_ShuaiFunction.AD_callback(() => {
                this.增加道具撤回次数();
            });

            // AdManager.是否看完广告 = false;
            // AdManager.Instance.showRewardVideoAd(AdManager.AdsIds.id_reward_vertical, {},
            //     () => {
            //         director.emit(SS_GameEventData.广告结束);

            //         this.增加道具撤回次数();
            //     },
            //     () => {
            //         director.emit(SS_GameEventData.广告结束);

            //         console.log("广告没看完！");
            //     },
            // );
        }

        SSS_AudioManager.PlayButtonAudio();
    }

    @SSS_Lockable(1000)
    On_Button_翻转() {
        if (JiuJiuXiaoDongWu_GameStorage.data.mode_1.item_翻转_number > 0) {
            JiuJiuXiaoDongWu_GameStorage.data.mode_1.item_翻转_number--;
            JiuJiuXiaoDongWu_GameStorage.saveData();
            this.Refresh();

            JiuJiuXiaoDongWu_GameManager.Instance.fanZhuan1();
        }
        else {
            SSS_ShuaiFunction.AD_callback(() => {
                this.增加道具翻转次数();
            });

            // AdManager.是否看完广告 = false;
            // AdManager.Instance.showRewardVideoAd(AdManager.AdsIds.id_reward_vertical, {},
            //     () => {
            //         director.emit(SS_GameEventData.广告结束);

            //         this.增加道具翻转次数();
            //     },
            //     () => {
            //         director.emit(SS_GameEventData.广告结束);

            //         console.log("广告没看完！");
            //     },
            // );
        }

        SSS_AudioManager.PlayButtonAudio();
    }

    增加道具撤回次数() {
        JiuJiuXiaoDongWu_GameStorage.data.mode_1.item_撤回_number += JiuJiuXiaoDongWu_GameData.item_撤回_add_number;
        JiuJiuXiaoDongWu_GameStorage.saveData();
        this.Refresh();
    }
    增加道具翻转次数() {
        JiuJiuXiaoDongWu_GameStorage.data.mode_1.item_翻转_number += JiuJiuXiaoDongWu_GameData.item_翻转_add_number;
        JiuJiuXiaoDongWu_GameStorage.saveData();
        this.Refresh();
    }

    On_Button_back() {
        // director.loadScene(SSS_Consts.SceneName.MainScene);
        SSS_GameEventManager.eventTarget.emit(SS_GameEventData.Menu);

        SSS_AudioManager.PlayButtonAudio();
    }

    On_Button_refresh() {
        director.loadScene(SSS_Consts.SceneName.JiuJiuXiaoDongWu_Game_Scene);

        SSS_AudioManager.PlayButtonAudio();
    }
}

// /** 调试 */
window["JiuJiuXiaoDongWu_GamePanel"] = JiuJiuXiaoDongWu_GamePanel;